package goiot.api;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import goiot.dto.JsonResp;
import goiot.dto.LoginForm;
/**
 * 用来定时刷新token
 */
public class WatchDog {
	private static final ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor();

	private WatchDog() {
	}
	
	public static String username;
	public static String password;
	public static Integer expires;
	

	public static void init() {
		LoginForm f = new LoginForm();
		f.setUsername(username);
		f.setPassword(password);
		f.setExpires(expires);
		ex.schedule(() -> {
			LoginApi loginApi = ApiHolder.loginApi;
			JsonResp<String> resp = loginApi.tokenRefresh();
			if (!Boolean.TRUE.equals(resp.getSuccess())) {
				loginApi.login(f);
			}
		}, 15, TimeUnit.SECONDS);
	}
}
